package com.stockz.ui.swing.charting.painter.indicator;

import java.awt.Graphics2D;

import com.stockz.ui.swing.charting.display.indicator.AbstractDisplayableIndicatorConfiguration;
import com.stockz.ui.swing.charting.util.MathUtil;
import com.stockz.ui.swing.charting.view.StockChartPanelState;
import com.stockz.ui.swing.charting.view.StockRectangle;

public class LineIndicatorPainter{
	
	public static void paint(Graphics2D g, AbstractDisplayableIndicatorConfiguration indicatorDisplay, double[] values) {
		StockRectangle rectangle = indicatorDisplay.getRectangle();
		if (rectangle != null && !rectangle.isEmpty()) {
			g.setClip(rectangle);
			g.setColor(indicatorDisplay.getColor());

			double min = rectangle.getMinimumValueVisible();
			double max = rectangle.getMaximumValueVisible();
			
			StockChartPanelState state = indicatorDisplay.getStockChartPanel().getStockChartPanelState();
			for (int i = state.getEndIndex() - 1; i >= state.getStartIndex(); i--) {
				if (i >= values.length 
						|| i <= 0
						|| values[i] > indicatorDisplay.getRectangle().getMaximumValueVisible()
						|| values[i] < indicatorDisplay.getRectangle().getMinimumValueVisible()) {
					continue;
				}

				g.drawLine(
						MathUtil.fitX(
								rectangle.x, 
								state.getTimeUnitSpace(),
								state.getEndIndex(), 
								i - 1), 
						MathUtil.fitY(
								rectangle.y,
								rectangle.y + rectangle.height,
								min,
								max,
								values[i - 1], 
								state.isSemiLog()), 
						MathUtil.fitX(
								rectangle.x, 
								state.getTimeUnitSpace(),
								state.getEndIndex(), 
								i),
						MathUtil.fitY(
								rectangle.y,
								rectangle.y + rectangle.height,
								min,
								max, 
								values[i],
								state.isSemiLog()));
			}
		}
	}
	
	
	public static void paint(Graphics2D g, AbstractDisplayableIndicatorConfiguration indicatorDisplay) {
		double[] values = indicatorDisplay.getValues();
		paint(g,indicatorDisplay,values);
	}
}
